//
//  IMSearchResultViewController.m
//  MyPhoneApp
//
//  Created by baken on 2016/10/24.
//  Copyright © 2016年 myApp. All rights reserved.
//

#import "IMSearchResultViewController.h"
#import "PPiFlatSegmentedControl.h"
#import "IMSearchViewModel.h"

#import "SearchNavigationView.h"
#import "SearchHelper.h"
#import "PCMainGoodsTableViewCell.h"
#import "PCGoodsDetailViewController.h"
#import "PCMainListModel.h"


@interface IMSearchResultViewController ()<UITableViewDelegate,UITableViewDataSource,searchDelegate>

//@property (nonatomic, strong) NSMutableArray *channelArray;

@property (nonatomic, strong) SearchNavigationView *titleView;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) IMSearchViewModel *searchVM;

@end

@implementation IMSearchResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorFromRGB(kBackGroundColor);
//    BOOL b = [[NSUserDefaults standardUserDefaults] boolForKey:@"isTest"];
//    if (b) {
//        self.channelArray = [NSMutableArray arrayWithObjects:@{@"name":@"资讯",@"icon":@""}, @{@"name":@"论坛",@"icon":@""}, @{@"name":@"手机",@"icon":@""}, nil];
//    } else {
//        self.channelArray = [NSMutableArray arrayWithObjects:@{@"name":@"资讯",@"icon":@""}, @{@"name":@"论坛",@"icon":@""}, @{@"name":@"商城",@"icon":@""}, @{@"name":@"手机",@"icon":@""}, nil];
//    }
    
    
    
    [self.view addSubview:self.titleView];
    [self.view addSubview:self.tableView];
    
    self.searchVM.keys = self.searchString;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.searchVM refreshDataCompletionHandle:^(NSError *error) {
            if (!error) {
                [self.tableView reloadData];
            }
            [self.tableView.mj_header endRefreshing];
        }];
        
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self.searchVM getMoreDataCompletionHandle:^(NSError *error) {
            if (!error) {
                [self.tableView reloadData];
            }
            [self.tableView.mj_footer endRefreshing];
        }];
    }];
    
    if (!IsStrEmpty(self.searchString)) {
        [self searchButtonClick:self.searchString];
    }

}

-(SearchNavigationView *)titleView{
    if (!_titleView) {
        _titleView = [[SearchNavigationView alloc] initWithFrame:CGRectMake(0, 30, kScreenWidth, 44)];
        _titleView.delegate = self;
        _titleView.searchText = self.searchString;
        _titleView.isSearchPop = YES;
    }
    return _titleView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.frame = CGRectMake(0, 74, kScreenWidth, kScreenHeight - 74);
        _tableView.separatorColor = UIColorFromRGB(kDetailLineColor);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setTableFooterView:[UIView new]];
        [_tableView setBackgroundColor:UIColorFromRGB(kAllWhiteColor)];
        [_tableView setSeparatorColor:UIColorFromRGB(kDetailLineColor)];
    }
    return _tableView;
}


-(IMSearchViewModel *)searchVM{
    if (!_searchVM) {
        _searchVM = [IMSearchViewModel new];
    }
    return _searchVM;
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

-(NSMutableArray *)results{
    if (!_results) {
        _results = [NSMutableArray array];
    }
    return _results;
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 110;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.searchVM.dataArr.count == 0) {
        return 1;
    }
    return self.searchVM.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.searchVM.dataArr.count == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"normalCell"];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
        label.text = @"无搜索结果";
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = UIColorFromRGB(kAllWhiteColor);
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = UIColorFromRGB(kLightFontColor);
        [cell addSubview:label];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        PCMainGoodsTableViewCell *cell = [PCMainGoodsTableViewCell cellWithTableView:tableView];
        PCMainSubListModel *model = [self.searchVM modelFromResultWithRow:indexPath.row];
        cell.model = model;
        return cell;
    
    }
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     PCMainSubListModel *model = [self.searchVM modelFromResultWithRow:indexPath.row];
    if (model) {
//        PCMainSubListModel *model = [self.pcMainVM goodsModelForRowInResultModel:indexPath.row];
        PCGoodsDetailViewController *vc = [[PCGoodsDetailViewController alloc] initWithNibName:@"PCGoodsDetailViewController" bundle:nil];
        vc.goodsID = model.num_iid;
//        vc.array = self.pcMainVM.goodsArray;
        [self.navigationController pushViewController:vc animated:YES];
    }

}



- (void)backButtonClick{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchButtonClick:(NSString *)searchStr{
    [self.titleView.textfield resignFirstResponder];
    if (searchStr) {
        self.searchString = searchStr;
    }
    
    SearchHelper *helper = [[SearchHelper alloc] init];
    if (![helper selectFromTableWithKeys:searchStr]) {
        [helper insertIntoTableWithKeys:searchStr];
    }
    
    self.searchVM.keys = self.searchString;
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.titleView.textfield resignFirstResponder];
}



@end
